package com.ideal.controller;

import com.ideal.dto.ResultDto;
import com.ideal.dto.config.req.*;
import com.ideal.dto.config.res.ConfigRspDto;
import com.ideal.model.Config;
import com.ideal.service.IAggregateConfigService;
import com.ideal.service.IConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;

/**
 * 配置Controller
 *
 * @author songfz
 * @date 2022-07-13
 */
@Controller
@RequestMapping("api/config")
public class ConfigController {
    @Autowired
    private IConfigService configService;
    @Autowired
    private IAggregateConfigService aggregateConfigService;

    /**
     * 查询配置列表
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto list(@RequestBody FileConfigPageReqDto pageReqDto) {
        return ResultDto.success(aggregateConfigService.page(pageReqDto));
    }


    /**
     * 获取配置基本信息
     */
    @RequestMapping(value = "base/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultDto getInfo(@PathVariable("id") Long id) {
        ConfigRspDto config = configService.findBaseByConfigId(id);
        return ResultDto.success(config);
    }

    /**
     * 获取配置详细信息
     */
    @RequestMapping(value = "detail/{version}", method = RequestMethod.GET)
    @ResponseBody
    public ResultDto detail(@PathVariable("version") String version) {
        return ResultDto.success(configService.findDetailByVersion(version));
    }

    /**
     * 新增配置
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto add(@RequestBody FileConfigDto config) throws UnsupportedEncodingException {
        if (config.getFileList().size() < 1) {
            ResultDto.fail("没有上传文件");
        }
        return ResultDto.success(aggregateConfigService.insertConfig(config));
    }

    /**
     * 修改配置
     */
    @RequestMapping(value = "edit", method = RequestMethod.PUT)
    @ResponseBody
    public ResultDto edit(Config config) {
        return ResultDto.success(configService.updateConfig(config));
    }

    /**
     * 已发布配置文件替换功能
     */
    @RequestMapping(value = "mod", method = RequestMethod.PUT)
    @ResponseBody
    public ResultDto mod(@RequestBody ConfigModReqDto reqDto) throws UnsupportedEncodingException {
        return ResultDto.success(aggregateConfigService.modConfig(reqDto));
    }

    /**
     * 确认页面配置内容修改
     * @param fileContentModReqDto
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateConfigContent", method = RequestMethod.PUT)
    public ResultDto updateConfigContent(@RequestBody FileContentModReqDto fileContentModReqDto) {

        return ResultDto.success(aggregateConfigService.updateConfigContent(fileContentModReqDto));
    }

    /**
     * 发布配置
     * @param version
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "releaseConfig/{version}", method = RequestMethod.GET)
    public ResultDto releaseConfig(@PathVariable("version") String version) {
        return ResultDto.success(aggregateConfigService.releaseConfig(version));

    }

    /**
     * 切换文件启用状态
     * @param configId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "checkFileSwitch/{configId}",method = RequestMethod.GET)
    public ResultDto checkFileSwitch(@PathVariable("configId") Long configId){
        return aggregateConfigService.checkFileSwitch(configId);
    }

    /**
     * 校验当前权限组下英文名称唯一
     * @param fileEnName
     * @param ruleGroup
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "verifyEnNameAndRule",method = RequestMethod.GET)
    public  ResultDto verifyEnNameAndRule(String fileEnName,String ruleGroup){
        return aggregateConfigService.verifyEnNameAndRule(fileEnName,ruleGroup);
}

}
